package zl.com.gz_2017029e;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import zl.com.gz_2017029e.adapter.Crossroads;
import zl.com.gz_2017029e.adapter.CrossroadsAdapter;

public class TrafficLightFrame extends Fragment implements View.OnClickListener{
//    private Toolbar toolbar;
    private Spinner spinner;
    private LinearLayout querytable;
    private int itemid=1;
    private Button queryBtn;
    private List<Crossroads> crossroadsList = new ArrayList<>();
    private RecyclerView crossroadRView;
    private CrossroadsAdapter crossroadsAdapter;

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.traffic_light_frame);
//        toolbar = (Toolbar)findViewById(R.id.main_toolbar);
//        setSupportActionBar(toolbar);
//        spinner = (Spinner)findViewById(R.id.trafficlight_spinner);
//    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.traffic_light_frame,container,false);
        spinner = (Spinner)view.findViewById(R.id.trafficlight_spinner);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                sort(crossroadsList,position);
//                String name = (String)spinner.getItemAtPosition(position)+position;
//                Toast.makeText(getContext(),name,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
//        querytable = (LinearLayout)view.findViewById(R.id.tra_querytable_layout);
        queryBtn = (Button)view.findViewById(R.id.trafficlight_query_btn);
        queryBtn.setOnClickListener(this);
        initdata();
        crossroadRView = (RecyclerView)view.findViewById(R.id.tra_querytable_recycler);
        crossroadsAdapter = new CrossroadsAdapter(crossroadsList);
        crossroadRView.setLayoutManager(new LinearLayoutManager(getContext())
//        {
//            @Override
//            public boolean canScrollVertically() {
//                return false;
//            }
//        }
        );
//        crossroadRView.setNestedScrollingEnabled(false);
//        crossroadRView.setHasFixedSize(true);
//        crossroadRView.setFocusable(false);
        crossroadRView.setAdapter(crossroadsAdapter);
        return view;

    }
    private void addView(){
//        LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.crossroads_item,null);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.crossroads_item,querytable,false);
        querytable.addView(view);
    }

    //recyclerView数据
    private void initdata(){
        Crossroads crossroads1 = new Crossroads(1,7,6,9);
        crossroadsList.add(crossroads1);
        Crossroads crossroads2 = new Crossroads(2,9,4,7);
        crossroadsList.add(crossroads2);
        Crossroads crossroads3 = new Crossroads(3,5,8,8);
        crossroadsList.add(crossroads3);
        Crossroads crossroads4 = new Crossroads(4,6,9,10);
        crossroadsList.add(crossroads4);
        Crossroads crossroads5 = new Crossroads(5,8,6,9);
        crossroadsList.add(crossroads5);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.trafficlight_query_btn:
                crossroadsAdapter.notifyDataSetChanged();
//                addView();
                break;
            case R.id.tra_querytable_recycler:
//                crossroadsAdapter.notifyDataSetChanged();
                break;
            default:break;
        }
    }

    //排序
    private void sort(List<Crossroads> list,int sortWay){
        switch (sortWay){
            case 0:
                //路口升序
                Collections.sort(list, new Comparator<Crossroads>() {
                    @Override
                    public int compare(Crossroads max, Crossroads min) {
                        return new Integer(max.getCrossroadsId()).compareTo(new Integer(min.getCrossroadsId()));
                    }
                });
                break;
            case 1:
                //路口降序
                Collections.sort(list, new Comparator<Crossroads>() {
                    @Override
                    public int compare(Crossroads max, Crossroads min) {
                        return new Integer(min.getCrossroadsId()).compareTo(new Integer(max.getCrossroadsId()));
                    }
                });
                break;
            case 2:
                //红灯升序
                Collections.sort(list, new Comparator<Crossroads>() {
                    @Override
                    public int compare(Crossroads max, Crossroads min) {
                        return new Integer(max.getRlight()).compareTo(new Integer(min.getRlight()));
                    }
                });
                break;
            case 3:
                //红灯降序
                Collections.sort(list, new Comparator<Crossroads>() {
                    @Override
                    public int compare(Crossroads max, Crossroads min) {
                        return new Integer(min.getRlight()).compareTo(new Integer(max.getRlight()));
                    }
                });
                break;
            case 4:
                //黄灯升序
                Collections.sort(list, new Comparator<Crossroads>() {
                    @Override
                    public int compare(Crossroads max, Crossroads min) {
                        return new Integer(max.getYlight()).compareTo(new Integer(min.getYlight()));
                    }
                });
                break;
            case 5:
                //黄灯降序
                Collections.sort(list, new Comparator<Crossroads>() {
                    @Override
                    public int compare(Crossroads max, Crossroads min) {
                        return new Integer(min.getYlight()).compareTo(new Integer(max.getYlight()));
                    }
                });
                break;
            case 6:
                //绿灯升序
                Collections.sort(list, new Comparator<Crossroads>() {
                    @Override
                    public int compare(Crossroads max, Crossroads min) {
                        return new Integer(max.getGlight()).compareTo(new Integer(min.getGlight()));
                    }
                });
                break;
            case 7:
                //绿灯降序
                Collections.sort(list, new Comparator<Crossroads>() {
                    @Override
                    public int compare(Crossroads max, Crossroads min) {
                        return new Integer(min.getGlight()).compareTo(new Integer(max.getGlight()));
                    }
                });
                break;
            default:
                break;
        }
    }


}
